<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_AjaxMoveFolderContentAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$sourceFolderId = $this->getRequestParameter("folderId");
        $rows = $this->getRequestParameter("row");
        $rows = is_array($rows) ? $rows : array($rows);
		$lang = $this->getContext()->getDefaultLang();
		$formValues = $this->getFormValues("form");
		$nbRows = count($rows);
        $errors = array();

		if(!is_null($sourceFolderId))
		{
			$sourceFolder = $this->getDocumentProvider()->getByModelAndUniqueId("modules_backoffice/folder", $sourceFolderId, $lang);
		}
		
		if($nbRows == 1)
		{
			$this->addToModel("uniqueRow", $this->getDocumentProvider()->getByUniqueId($rows[0], $lang));
		}

		if(isset($formValues['destFolderId']))
		{
			if(!is_null($sourceFolder))
			{
                $dbProvider = f_database_Provider::getInstance();
                
                try
                {
                    foreach($rows as $docId)
                    {
                        $dbProvider->beginTransaction();
                        $document = $this->getDocumentProvider()->getByUniqueId($docId, $lang);
                        $destFolder = $this->getDocumentProvider()->getByUniqueId($formValues['destFolderId'], $lang);
                        
                        $relationType = $document->getDocumentTypeName();

    					if($relationType == "media")
    					{
    						$moveError = $document->moveMedia($destFolder);
    					}

      					if($moveError === true)
    					{
                            f_relation_ManagerNew::getInstance($sourceFolder)->clearRelation($document,$relationType);
                            f_relation_ManagerNew::getInstance($destFolder)->addRelation($document,$relationType);
                            $dbProvider->commit();
    					}
    					else
    					{
    						$errors["failed"][] = $moveError;
    					}
                    }
                }
                catch(BaseException $e)
                {
                    $dbProvider->rollBack($e);
                }                
			
				if(!isset($errors["failed"]))
				{
					$errors["status"] = 'success';
                    if(isset($formValues['goToDestFolder']))
                    {
                        $errors['dest'] = $destFolder->getEditUrl($lang);
                    }
                    else
                    {
                        $errors['dest'] = 'currentPage';
                    }
				}
			}
		}
		
		if(!isset($errors["status"]))
		{
            $folderOptionsTab = m_backoffice_services_TreeService::getInstance()->getSelectArray("medias", array("modules_backoffice/Folder"), null, false, true, 'medias', 'add');
		}

		$this->addToModel("sourceFolderId", $sourceFolderId);
		$this->addToModel("errors", $errors);
		$this->addToModel("folder", $sourceFolder);
		$this->addToModel("folderOptionsTab", $folderOptionsTab);
		$this->addToModel("lang", $lang);
		$this->addToModel("rows", $rows);
		$this->addToModel("nbRows", $nbRows);
		$this->setView('success');
	}	
}